﻿using System;
using System.Web;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfShopScoreCardCreate : msBsc.Cls.BPDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
                string strsid = this.Request.QueryString.Get("s");
                this.ExecJScript("$('#txts').val('" + strsid + "');");
                if (string.IsNullOrEmpty(strsid))
                {
                    strsid = "";
                }
                if (strsid == "0")
                {
                    this.ExecJScript("$('.current').removeClass('current');");
                    this.ExecJScript("$('.switch-cart-2').addClass('current');");
                    this.ExecJScript("$('#J_FilterBar .floater').animate({ left: 134 * 1 + 'px' }, 100);");
                }
                else if (strsid == "1")
                {
                    this.ExecJScript("$('.current').removeClass('current');");
                    this.ExecJScript("$('.switch-cart-3').addClass('current');");
                    this.ExecJScript("$('#J_FilterBar .floater').animate({ left: 134 * 2 + 'px' }, 100);");
                }
                else if (strsid == "2")
                {
                    this.ExecJScript("$('.current').removeClass('current');");
                    this.ExecJScript("$('.switch-cart-4').addClass('current');");
                    this.ExecJScript("$('#J_FilterBar .floater').animate({ left: 134 * 3 + 'px' }, 100);");
                }
                dodoloadlist(strsid, "", "", "", "", "");
            }

        }
        private void BindControls()
        {
            if (!IsPostBack)
            { 
            }
        }
        private void ShowStatic()
        {
            using (msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo odataobj = new AppLogic.DataAlc.CBizShpCommercialTenantInfo())
            {
                DataSet ds = odataobj.DataObj.QueryBySQL(@"
    SELECT
    COUNT1=COUNT(*)
    ,COUNT2=ISNULL(SUM(CASE WHEN USE_DT IS NULL THEN 1 ELSE 0 END),0)
    ,COUNT3=ISNULL(SUM(CASE WHEN USE_DT IS NOT NULL THEN 1 ELSE 0 END),0)
    FROM SHP_SCORE_CARD A  
    WHERE
    (A.CREATE_ACCOUNT_NO='" + msBsc.SvRef.UserInfo.OperDealerId.Split(',')[0] + @"')
    ");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    this.ExecJScript("$('#emD1').html('" + ds.Tables[0].Rows[0]["COUNT1"].ToString() + "');");
                    this.ExecJScript("$('#emD2').html('" + ds.Tables[0].Rows[0]["COUNT2"].ToString() + "');");
                    this.ExecJScript("$('#emD3').html('" + ds.Tables[0].Rows[0]["COUNT3"].ToString() + "');"); 
                }
                this.ExecJScript("$('#J_SmallTotal').html('" + odataobj.GetShopScoreAllowUseQty(msBsc.SvRef.UserInfo.OperDealerId.Split(',')[0]).ToString() + "');");
            }
        }

        private void dodoloadlist(string strSid
            , string strDtStart, string strDtEnd
            , string CurrPageNum, string strPageRowsCount, string strSort)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpOrderRate oCDbAccess = new msBsc.AppLogic.DataAlc.CBizShpOrderRate())
            {
                string strSql = @"
    SELECT
    A.* 
    FROM SHP_SCORE_CARD A  
    WHERE
    (A.CREATE_ACCOUNT_NO='" + msBsc.SvRef.UserInfo.OperDealerId.Split(',')[0] + @"')
    "; 
                DataSet ds = oCDbAccess.DataObj.QueryBySQL(strSql);
                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", ""
                    , (CurrPageNum == string.Empty ? 1 : int.Parse(CurrPageNum))
                    , (strPageRowsCount == string.Empty ? 20 : int.Parse(strPageRowsCount))
                    , (strSort == string.Empty ? "INITIAL_DT DESC" : strSort));
                this.ExecJScript(@" var data= " + strjsontem1 + @" ; 
                updateTableHtml( data);");
                ShowStatic();
            }
        }


        public override void DoAction()
        {
            if (this.strAction == "doloadlist")
            {
                dodoloadlist(this.GetParamHtValue("TRADING_SID")
                    , this.GetParamHtValue("INITIAL_DT_S")
                    , this.GetParamHtValue("INITIAL_DT_E")
                    , this.GetParamHtValue("NEW_PAGE_NUM")
                    , this.GetParamHtValue("PAGE_ROW_COUNT")
                    , this.GetParamHtValue("PAGE_ROW_SORT"));
            }
            else if (this.strAction == "dochongzhi")
            {
                int itm1 = 0;
                if (!int.TryParse(this.GetParamHtValue("txtSetScoreQty"), out itm1))
                {
                    Alert("请输入正确的积分数量！");
                    return;
                }

                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    using (msBsc.AppLogic.DataAlc.CBizShpScoreCard objlog = new AppLogic.DataAlc.CBizShpScoreCard())
                    {
                        objlog.CreateScoreCard(itm1);

                        this.ExecJScript("layerform && layer.close(layerform);loadlist();");
                    }
                    ts.Complete();
                }
            } 
        }
    }
}